﻿#region License
// (c) Garry McGlennon 2009
// 
// This project is made available under the Microsoft Public License (Ms-PL)
// Code is provided as is and with no warrenty – Use at your own risk
// 
// View the project and the latest code at http://dnpframework.codeplex.com/
// 
// All other rights reserved.
// 
// blog: www.dotNetProfessional.com/blog/
#endregion
using dnp.Framework.Database.DatabaseExplorer;
using MbUnit.Framework;

namespace dnp.Framework.UnitTest.Database.DatabaseExplorer
{
    [TestFixture]
    public class RoutineTest
    {
        private Framework.Database.DatabaseExplorer.Database _database;

        [TestFixtureSetUp]
        public void Setup()
        {
            _database = new Framework.Database.DatabaseExplorer.Database("ReferenceSchema", null);
        }

        [Test]
        public void Name()
        {
            Routine t = _database.Routines["dbo.fn_RetrieveAllDataTypes"];

            Assert.AreEqual("fn_RetrieveAllDataTypes", t.Name);
        }

        [Test]
        public void Alias()
        {
            // Check that aliased Routine names have thier alias
            //Assert.AreEqual("fn_RetrieveAllDataTypes", _database.Routines["dbo.fn_RetrieveAllDataTypes"].Alias);
            // Check that non-aliased Routines have the name of the Routine
            Assert.AreEqual("fn_RetrieveAllDataTypes", _database.Routines["dbo.fn_RetrieveAllDataTypes"].Alias);
        }

        [Test]
        public void FullName()
        {
            Routine t = _database.Routines["dbo.fn_RetrieveAllDataTypes"];
            Assert.AreEqual(t.Schema + "." + t.Name, t.FullName);
        }

        [Test]
        public void Schema()
        {
            Assert.AreEqual("dbo", _database.Routines["dbo.fn_RetrieveAllDataTypes"].Schema);
            //Assert.AreEqual("test", _database.Routines["test.Dealer"].Schema);
        }

        /// <summary>
        /// Test that the SQL of the Routine can be extracted
        /// </summary>
        [Test]
        public void Text()
        {
            string expectedSQL =
@"
/*===============================================================================
 * Company: DotNetProfessional
 * Project: ReferenceSchema
 * Model: dnpModel v1.0.0
 * Created: 6/11/2008 4:29:40 PM
 *===============================================================================
 * Copyright 2008 DotNetProfessional
 *===============================================================================
 */
create FUNCTION [dbo].[fn_RetrieveAllDataTypes]
()
RETURNS TABLE
AS 
RETURN
(
	SELECT
		AllDataTypes.[BigInt] [BigInt],
		AllDataTypes.[Binary] [Binary],
		AllDataTypes.[Bit] [Bit],
		AllDataTypes.[Char] [CharX],
		AllDataTypes.[DateTime] [DateTime],
		AllDataTypes.[Decimal] [Decimal],
		AllDataTypes.[Float] [Float],
		AllDataTypes.[Image] [Image],
		AllDataTypes.[Int] [Int],
		AllDataTypes.[Money] [Money],
		AllDataTypes.[Nchar] [Nchar],
		AllDataTypes.[Ntext] [Ntext],
		AllDataTypes.[Numeric] [Numeric],
		AllDataTypes.[Nvarchar] [Nvarchar],
		AllDataTypes.[NVarcharMax] [NVarcharMax],
		AllDataTypes.[Real] [Real],
		AllDataTypes.[SmallDateTime] [SmallDateTime],
		AllDataTypes.[SmallInt] [SmallInt],
		AllDataTypes.[SmallMoney] [SmallMoney],
		AllDataTypes.[SqlVariant] [SqlVariant],
		AllDataTypes.[Text] [Text],
		AllDataTypes.[Timestamp] [Timestamp],
		AllDataTypes.[TinyInt] [TinyInt],
		AllDataTypes.[UniqueIdentifier] [UniqueIdentifier],
		AllDataTypes.[VarBinary] [VarBinary],
		AllDataTypes.[VarBinaryMax] [VarBinaryMax],
		AllDataTypes.[VarChar] [VarChar],
		AllDataTypes.[VarCharMax] [VarCharMax],
		AllDataTypes.[Xml] [Xml]
	FROM dbo.AllDataTypes AllDataTypes
		
)
";

            string actualSQL = _database.Routines["dbo.fn_RetrieveAllDataTypes"].Text;
            Assert.AreEqual(expectedSQL, actualSQL);
        }
    }
}
